package com.jy.rhin.support.util;

import org.apache.commons.lang.StringUtils;

/**
 * @Description: 脱敏工具类
 * @Author: lww
 * @Date: 2022/6/24 15:50
 */
public class DesensitizationUtils {

    private DesensitizationUtils() {
    }

    private static final int INDEX = 2;

    /**
     * 姓名脱敏
     * 一位时不脱敏，两位时脱敏姓后面，两位以上保留首尾中间脱敏
     *
     * @param name
     * @return
     */
    public static String nameDesensitization(String name) {
        if (StringUtils.isEmpty(name)) {
            return name;
        }
        char[] sArr = name.toCharArray();
        if (sArr.length == INDEX) {
            return sArr[0] + "*";
        } else if (sArr.length > INDEX) {
            for (int i = 1; i < sArr.length - 1; i++) {
                sArr[i] = '*';
            }
            return new String(sArr);
        }
        return name;
    }

    /**
     * 身份证脱敏
     * 保留前三后二
     *
     * @param card
     * @return
     */
    public static String cardDesensitization(String card) {
        if (StringUtils.isEmpty(card)) {
            return card;
        }
        return StringUtils.left(card, 3).concat("*************").concat(StringUtils.right(card, 2));
    }
}
